// AddTodoActivity.java
package com.example.finalproject;

import android.app.DatePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import java.util.Calendar;
import android.app.TimePickerDialog;
import java.text.SimpleDateFormat;
import java.util.Locale;

public class AddTodoActivity extends AppCompatActivity {
    private EditText todoContentEditText;
    private EditText remarkEditText;
    private TextView selectedDateTimeTextView;
    private RadioGroup priorityRadioGroup;
    private int priority = 2; // 默认中优先级
    private Calendar selectedCalendar = Calendar.getInstance();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_todo);

        // 初始化UI组件
        todoContentEditText = findViewById(R.id.et_todo_content);
        remarkEditText = findViewById(R.id.et_remark);
        selectedDateTimeTextView = findViewById(R.id.tv_selected_date_time);
        priorityRadioGroup = findViewById(R.id.rg_priority);

        // 日期选择按钮
        findViewById(R.id.btn_select_date).setOnClickListener(v -> showDatePickerDialog());

        // 时间选择按钮
        findViewById(R.id.btn_select_time).setOnClickListener(v -> showTimePickerDialog());

        // 优先级选择监听
        priorityRadioGroup.setOnCheckedChangeListener((group, checkedId) -> {
            if (checkedId == R.id.rb_low) {
                priority = 1;
            } else if (checkedId == R.id.rb_medium) {
                priority = 2;
            } else if (checkedId == R.id.rb_high) {
                priority = 3;
            }
        });

        // 确认添加按钮
        findViewById(R.id.btn_confirm).setOnClickListener(v -> {
            String content = todoContentEditText.getText().toString().trim();
            String remark = remarkEditText.getText().toString().trim();

            if (content.isEmpty()) {
                Toast.makeText(this, "待办内容不能为空", Toast.LENGTH_SHORT).show();
                return;
            }

            // 生成格式化的待办事项
            String formattedTodo = buildFormattedTodo(content, remark);

            // 返回数据给主界面
            Intent resultIntent = new Intent();
            resultIntent.putExtra("todo_content", formattedTodo);
            setResult(RESULT_OK, resultIntent);
            finish();
        });
    }

    // 显示日期选择对话框
    private void showDatePickerDialog() {
        Calendar calendar = Calendar.getInstance();
        if (!isDateEmpty()) {
            calendar.setTime(selectedCalendar.getTime());
        }

        DatePickerDialog dialog = new DatePickerDialog(
                this,
                (view, year, month, dayOfMonth) -> {
                    selectedCalendar.set(Calendar.YEAR, year);
                    selectedCalendar.set(Calendar.MONTH, month);
                    selectedCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                    updateDateTimeDisplay();
                },
                calendar.get(Calendar.YEAR),
                calendar.get(Calendar.MONTH),
                calendar.get(Calendar.DAY_OF_MONTH)
        );
        dialog.show();
    }

    // 显示时间选择对话框（精确到分钟）
    private void showTimePickerDialog() {
        Calendar calendar = Calendar.getInstance();
        if (!isDateEmpty()) {
            calendar.setTime(selectedCalendar.getTime());
        }

        TimePickerDialog dialog = new TimePickerDialog(
                this,
                (view, hourOfDay, minute) -> {
                    selectedCalendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
                    selectedCalendar.set(Calendar.MINUTE, minute);
                    updateDateTimeDisplay();
                },
                calendar.get(Calendar.HOUR_OF_DAY),
                calendar.get(Calendar.MINUTE),
                true // 24小时制
        );
        dialog.show();
    }

    // 更新日期时间显示
    private void updateDateTimeDisplay() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.getDefault());
        selectedDateTimeTextView.setText("已选择时间：" + sdf.format(selectedCalendar.getTime()));
    }

    // 判断是否选择了日期
    private boolean isDateEmpty() {
        Calendar temp = Calendar.getInstance();
        return selectedCalendar.get(Calendar.YEAR) == temp.get(Calendar.YEAR) &&
                selectedCalendar.get(Calendar.MONTH) == temp.get(Calendar.MONTH) &&
                selectedCalendar.get(Calendar.DAY_OF_MONTH) == temp.get(Calendar.DAY_OF_MONTH) &&
                selectedCalendar.get(Calendar.HOUR_OF_DAY) == temp.get(Calendar.HOUR_OF_DAY) &&
                selectedCalendar.get(Calendar.MINUTE) == temp.get(Calendar.MINUTE);
    }

    // 构建格式化的待办事项字符串
    private String buildFormattedTodo(String content, String remark) {
        StringBuilder sb = new StringBuilder();

        // 添加优先级标签
        switch (priority) {
            case 1:
                sb.append("[低]");
                break;
            case 2:
                sb.append("[中]");
                break;
            case 3:
                sb.append("[高]");
                break;
        }

        // 添加日期时间标签
        if (!isDateEmpty()) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.getDefault());
            sb.append("[");
            sb.append(sdf.format(selectedCalendar.getTime()));
            sb.append("] ");
        }

        // 添加内容
        sb.append(content);

        // 添加备注（如果有）
        if (!remark.isEmpty()) {
            sb.append(" [备注:");
            sb.append(remark);
            sb.append("]");
        }

        return sb.toString();
    }
}